package com.pbs.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class JedisConfig {
    @Value("${redis.host}")
    String host ;
    @Value("${redis.port}")
    int port ;
    @Value("${redis.password}")
    String pass;
    @Value("${redis.database}")
    int database;
    @Value("${redis.timeout}")
    int timeout;
    @Value("${redis.jedis.pool.min-idle}")
    int minIdle;
    @Value("${redis.jedis.pool.max-idle}")
    int maxIdle;
    @Value("${redis.jedis.pool.max-active}")
    int maxTotal;
    @Value("${redis.jedis.pool.max-wait}")
    long maxWaitMillis;
    @Value("${redis.jedis.testOnBorrow}")
    boolean testOnBorrow;

    @Bean
    public JedisPool jedisPoolFactory() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setMaxWaitMillis(maxWaitMillis);
        config.setTestOnBorrow(testOnBorrow);
        JedisPool jedisPool = null;
        if (StringUtils.isEmpty(pass)) {
            jedisPool = new JedisPool(config, host, port, timeout, null,database);
        }else {
            jedisPool = new JedisPool(config, host, port, timeout, pass,database);
        }
        return jedisPool;
    }
}
